package com.maimang.platform.activiti.controller;

import com.maimang.platform.activiti.dto.ProcessDefinitionDto;
import com.maimang.platform.activiti.service.ActProcessService;
import com.maimang.platform.annotation.Log;
import com.maimang.platform.base.BaseController;
import com.maimang.platform.base.Result;
import com.maimang.platform.enums.BusinessType;
import com.maimang.platform.lang.StringUtils;
import com.maimang.platform.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.activiti.engine.repository.Model;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author Rocky
 * @version 1.0.0
 * @classname ActProcessController
 * @description 流程管理
 * @date 2019-04-22 20:17
 * @email jintaozhao@qq.com
 */
@Controller
@RequestMapping("/activiti/process")
@Api(value = "流程管理", description = "activiti流程管理操作接口")
public class ActProcessController extends BaseController {

	private String prefix = "activiti/process";

	@Autowired
	private ActProcessService actProcessService;

	@RequiresPermissions("activiti:process:view")
	@GetMapping
	public String process() {
		return prefix + "/process";
	}

	@ApiOperation(value = "流程列表", notes = "参数：流程实体数据对象")
	@RequiresPermissions("activiti:process:list")
	@PostMapping("list")
	@ResponseBody
	public TableDataInfo list(ProcessDefinitionDto processDefinitionDto) {
		return actProcessService.selectProcessDefinitionList(processDefinitionDto);
	}

	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}


	/**
	 * 添加流程文件
	 *
	 * @param catagory 种类
	 * @param file     流程文件
	 * @return
	 * @throws IOException
	 */
	@ApiOperation(value = "添加流程", notes = "参数：catagory流程类型，file流程文件")
	@RequiresPermissions("activiti:process:add")
	@Log(title = "流程管理", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result saveProcess(@RequestParam("catagory") String catagory, @RequestParam("file") MultipartFile file) throws IOException {
		InputStream inputStream = file.getInputStream();
		String fileName = file.getOriginalFilename();

		return actProcessService.saveNameDeplove(inputStream, fileName, catagory);
	}


	/**
	 * 将部署的流程转换为模型
	 *
	 * @param processId 流程id
	 * @return
	 */
	@ApiOperation(value = "流程转换", notes = "根据流程id将部署的流程转换为模型")
	@RequiresPermissions("activiti:process:model")
	@GetMapping(value = "/convertToModel/{processId}")
	@ResponseBody
	public Result convertToModel(@PathVariable("processId") String processId) {
		try {
			Model model = actProcessService.convertToModel(processId);
			String msg = StringUtils.format("转换模型成功，模型编号[{}]", model.getId());
			return success(msg);
		} catch (Exception e) {
			return error("模型转换失败" + e.getMessage());
		}
	}

	@GetMapping(value = "/resource/{imageName}/{deploymentId}")
	public void viewImage(@PathVariable("imageName") String imageName, @PathVariable("deploymentId") String deploymentId, HttpServletResponse response) {
		try {
			InputStream inputStream = actProcessService.findImageStream(deploymentId, imageName);
			for (int bit = -1; (bit = inputStream.read()) != -1; ) {
				response.getOutputStream().write(bit);
			}
			inputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	/**
	 * 删除流程
	 *
	 * @param ids processId数组
	 * @return
	 */
	@RequiresPermissions("activiti:process:remove")
	@PostMapping("/remove")
	@ResponseBody
	public Result removeProcess(String ids) {
		boolean result = actProcessService.deleteProcessDefinitionByDeploymentIds(ids);
		return toResult(result);
	}
}
